package com.iotproject.studyservice.controller.admin;

import com.iotproject.commonutils.R;
import com.iotproject.studyservice.entity.StudyListening;
import com.iotproject.studyservice.entity.vo.admin.ListeningQuery;
import com.iotproject.studyservice.service.StudyListeningService;
import com.iotproject.studyservice.service.StudyListeningTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("admin/studyservice/studyListening/")
public class AdminStudyListeningController {

    @Autowired
    StudyListeningService studyListeningService;

    @Autowired
    StudyListeningTypeService studyListeningTypeService;

    //条件查询带分页的方法
    @PostMapping("pageListeningCondition/{current}/{limit}")
    public R pageListeningCondition(@PathVariable("current") long current,
                               @PathVariable("limit") long limit,
                               @RequestBody(required = false) ListeningQuery listeningQuery) {

        Page<StudyListening> listeningPage = new Page<>(current, limit);
        QueryWrapper<StudyListening> listeningQueryWrapper = new QueryWrapper<>();

        String name = listeningQuery.getName();
        String type = listeningQuery.getTypeId();
        if (!StringUtils.isEmpty(name)) {
            listeningQueryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(type)) {
            listeningQueryWrapper.eq("type_id", type);
        }
        studyListeningService.page(listeningPage, listeningQueryWrapper);
        long total = listeningPage.getTotal();
        List<StudyListening> records = listeningPage.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    @DeleteMapping("deleteListeningById/{id}")
    public R deleteListeningById(@PathVariable("id") String id) {
        boolean removeById = studyListeningService.removeById(id);
        if (removeById) {
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("getListeningById/{id}")
    public R getListeningById(@PathVariable("id") String id) {
        StudyListening listening = studyListeningService.getById(id);
        return R.ok().data("listening", listening);
    }

    @PostMapping("addListening")
    public R addListening(@RequestBody StudyListening studyListening) {
        boolean save = studyListeningService.save(studyListening);
        if (save) {
            return R.ok();
        }
        return R.error();
    }

    @PostMapping("updateListening")
    public R updateListening(@RequestBody StudyListening studyListening) {
        boolean updateById = studyListeningService.updateById(studyListening);
        if (updateById) {
            return R.ok();
        }
        return R.error();
    }

    //传入Excel文件，将内容存储到数据库
    @PostMapping("addListeningByExcel")
    public R upload(MultipartFile file) {
        studyListeningService.importStudyListeningData(file, studyListeningService, studyListeningTypeService);
        return R.ok();
    }

}
